
#include "com_title.h"
#include "root.h"
#include "pt_network.h"
#include "sys_font.h"
#include "pt_fs.h"
#include "pt_info.h"
#include "pt_dev.h"
#include "ui_common.h"

char* title_point[2] =
{
    "Dot_Default.png",
    "Dot_Selected.png"
};

static char* title_home[2] =
{
    "Home_Default.png",
    "Home_Selected.png"
};

static top_title_t top_title;

void title_left(void)
{
    top_title.title_left_info = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(top_title.title_left_info, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(top_title.title_left_info,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(top_title.title_left_info,LV_OPA_90,0);
    midea_ui_safe_set_text(top_title.title_left_info," ");
    // midea_ui_safe_obj_align(top_title.title_left_info,LV_ALIGN_TOP_LEFT,10,11);
  
    lv_obj_set_pos(top_title.title_left_info,6,10);
    lv_obj_set_size(top_title.title_left_info,130,30);

    lv_obj_set_style_text_align(top_title.title_left_info,LV_TEXT_ALIGN_LEFT,0);
    //lv_label_set_long_mode(top_title.title_left_info, LV_LABEL_LONG_SCROLL_CIRCULAR);
    // lv_label_set_long_mode(top_title.title_left_info, LV_LABEL_LONG_DOT);

    
    /* 创建标题切换定时器timer */
    top_title.title_name_timer = lv_timer_create(timer_title_name,500,  NULL);
    // lv_timer_reset(title_name_timer);
}

void timer_title_name(lv_timer_t * timer)
{
    char cur_id = 0;
    char txt_buff[128];
    root_link_t*  obj = NULL;

    cur_id = get_title_page_id();

    memset(txt_buff,'\0',sizeof(txt_buff));

    if(cur_id == 0)
    {
        snprintf(txt_buff,sizeof(txt_buff),"%s","智能场景");
    }
    else if(cur_id == 1)
    {
        snprintf(txt_buff,sizeof(txt_buff),"%s","主页");
    }
    else{
        obj = root_page_find_index(cur_id);
        if(!obj){
            return ;
        }

        char* name_start = (char*)malloc(13);
        char* name_end   = (char*)malloc(13);
    
        int total_len = ui_util_get_wstr_len(obj->e_attr.name);
        if(total_len>6)
        {
            ui_util_sub_pre_wstr(obj->e_attr.name,4,&name_start);
            ui_util_sub_subfix_wstr(obj->e_attr.name,total_len-1,&name_end);
    
            snprintf(txt_buff,128,"%s...%s",name_start,name_end);
        }
        else
        {
            snprintf(txt_buff,128,"%s",obj->e_attr.name);
        }

        free(name_start);
        free(name_end);
    }
    
    if(top_title.title_left_info){
        midea_ui_safe_set_text(top_title.title_left_info,txt_buff);
    }
}


void set_title_page_id(int id)
{
    top_title.page_id = id;
}

int get_title_page_id(void)
{
    return top_title.page_id;
}

/*
参数：
obj：页面对象
curr_pos：当前设备页位置，对应root.h定义，如下：
设置页1     0
设置页2     1
屏保页      2
场景页      3
主页        4
设备页1     5
设备页2     6
    ......
*/
void title_middle(void)
{
    int i = 0;
    int dev_num = pt_dev_num_get();

    top_title.top_icon = lv_obj_create(lv_layer_top());
    midea_ui_safe_remove_style_all(top_title.top_icon);
    midea_ui_safe_set_size(top_title.top_icon, 180, 15);
    midea_ui_safe_obj_align(top_title.top_icon,LV_ALIGN_TOP_MID,-30,15);

    // int16_t all_length = ICON_INTERVAL + (ICON_INTERVAL*dev_num);   //所有小图标累计长度
    int16_t pos_x = -(ICON_INTERVAL + (ICON_INTERVAL*dev_num)/2);      //x轴起始坐标

    //场景页
    top_title.top_icon_point = lv_img_create(top_title.top_icon);
    midea_ui_safe_obj_align(top_title.top_icon_point,LV_ALIGN_TOP_MID,pos_x,0);
    midea_ui_set_img_src(top_title.top_icon_point, pt_fs_get(title_point[0]));

    //主页 创建时在主页

    pos_x += ICON_INTERVAL;
    top_title.top_icon_home = lv_img_create(top_title.top_icon);
    midea_ui_safe_obj_align(top_title.top_icon_home,LV_ALIGN_TOP_MID,pos_x,0);
    midea_ui_set_img_src(top_title.top_icon_home, pt_fs_get(title_home[1]));

    for(i=0;i<TITLE_MAX_POINT;i++)
    {
        pos_x += ICON_INTERVAL;   
        top_title.top_icon_point_dev[i] = lv_img_create(top_title.top_icon);
        midea_ui_safe_obj_align(top_title.top_icon_point_dev[i],LV_ALIGN_TOP_MID,pos_x,0);
        midea_ui_set_img_src(top_title.top_icon_point_dev[i], pt_fs_get(title_point[0]));  

        if(i >= dev_num)      
        {
            midea_ui_safe_add_flag(top_title.top_icon_point_dev[i],LV_OBJ_FLAG_HIDDEN);
        }
    }

    /* 创建小圆点切换定时器timer */
    top_title.title_point_timer = lv_timer_create(point_timer, 300,  NULL);
}

void point_timer(lv_timer_t * timer)
{
    int i = 0;
    static int dev_num  = 0;
    static int curr_pos = 0;

    if((curr_pos != get_title_page_id())||
       (dev_num  != pt_dev_num_get()))
    {
        dev_num  = pt_dev_num_get();
        curr_pos = get_title_page_id();
    }
    else{
        return ;
    }
    
    if((PAGE_HOME_ID + dev_num) < curr_pos)
    {
        curr_pos = (PAGE_HOME_ID + dev_num);
    }

    int16_t pos_x = -(ICON_INTERVAL + (ICON_INTERVAL*dev_num)/2);      //x轴起始坐标

    midea_ui_safe_obj_align(top_title.top_icon_point,LV_ALIGN_TOP_MID,pos_x,0);

    pos_x += ICON_INTERVAL;
    midea_ui_safe_obj_align(top_title.top_icon_home,LV_ALIGN_TOP_MID,pos_x,0);
    
    //debug_d("curr_pos=%d:%d\n",curr_pos,get_title_page_id()); 
    //debug_d("dev_num=%d:%d\n",dev_num,pt_dev_num_get()); 

    // 场景页
    // if(PAGE_SCENE_ID == curr_pos)
    //     midea_ui_set_img_src(top_title.top_icon_point, pt_fs_get(title_point[1]));
    // else
    //     midea_ui_set_img_src(top_title.top_icon_point, pt_fs_get(title_point[0]));

    midea_ui_set_img_src(top_title.top_icon_point, pt_fs_get(title_point[0]));   

    // 主页
    if(PAGE_HOME_ID == curr_pos)
        midea_ui_set_img_src(top_title.top_icon_home, pt_fs_get(title_home[1]));
    else
        midea_ui_set_img_src(top_title.top_icon_home, pt_fs_get(title_home[0]));

  
    for(i=0;i<TITLE_MAX_POINT;i++)
    {
        pos_x += ICON_INTERVAL;   
        midea_ui_safe_obj_align(top_title.top_icon_point_dev[i],LV_ALIGN_TOP_MID,pos_x,0);
        
        if(i == (curr_pos -5))
            midea_ui_set_img_src(top_title.top_icon_point_dev[i], pt_fs_get(title_point[1]));
        else
            midea_ui_set_img_src(top_title.top_icon_point_dev[i], pt_fs_get(title_point[0])); 

        if(i >= dev_num)      
        {
            midea_ui_safe_add_flag(top_title.top_icon_point_dev[i],LV_OBJ_FLAG_HIDDEN);
        }
        else{
            midea_ui_safe_clear_flag(top_title.top_icon_point_dev[i],LV_OBJ_FLAG_HIDDEN);
        }
    }
}

void title_right(void)
{
    top_title.title_right_temp = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(top_title.title_right_temp, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(top_title.title_right_temp,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(top_title.title_right_temp,LV_OPA_90,0);
    lv_obj_set_pos(top_title.title_right_temp,285,11);
    
    if(NOT_GET_VAL == pt_temperature_get()){
        lv_label_set_text_fmt(top_title.title_right_temp, "室外 --°C");
    }else{
        lv_label_set_text_fmt(top_title.title_right_temp, "室外%d°C", pt_temperature_get());
    }
    

    top_title.wifi_icon = lv_img_create(lv_layer_top());
    midea_ui_set_img_src(top_title.wifi_icon, pt_fs_get("NoNetwork.png"));
    lv_obj_set_pos(top_title.wifi_icon,345,4);

    if(!pt_network_get())
    {
        midea_ui_safe_add_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
        midea_ui_safe_clear_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }
    else
    {
        midea_ui_safe_clear_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
        midea_ui_safe_add_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }

    /* 创建本地时间定时器timer */
    top_title.local_timer = lv_timer_create(timer_right,1000,NULL);//定时周期500ms
    // lv_timer_reset(local_timer);
    
    get_sys_time();
    top_title.title_hour = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(top_title.title_hour, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(top_title.title_hour,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(top_title.title_hour,LV_OPA_90,0);
    lv_obj_set_pos(top_title.title_hour,378,11);
    lv_label_set_text_fmt(top_title.title_hour, "| %02d", pt_hour_get());

    top_title.title_right_colon = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(top_title.title_right_colon, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(top_title.title_right_colon,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(top_title.title_right_colon,LV_OPA_90,0);
    lv_obj_set_pos(top_title.title_right_colon,414,10);
    lv_label_set_text_fmt(top_title.title_right_colon, ":");

    top_title.title_min = lv_label_create(lv_layer_top());
    lv_obj_set_style_text_font(top_title.title_min, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(top_title.title_min,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_opa(top_title.title_min,LV_OPA_90,0);
    lv_obj_set_pos(top_title.title_min,421,11);
    lv_label_set_text_fmt(top_title.title_min, "%02d", pt_minute_get());
}


void timer_right(lv_timer_t * timer)
{
    static bool two_val = 0;
    static int  min     = 0;

    if(top_title.title_state == TITLE_HIDE_ALL)
        return;

    if(!top_title.local_timer)
        return ;

    if(!top_title.title_right_colon)
        return ;

    if(!top_title.title_hour)
        return ;

    if(!top_title.title_min)
        return ;

    two_val = !two_val;

    if(two_val){
        lv_label_set_text_fmt(top_title.title_right_colon, ":");}
    else{
        lv_label_set_text_fmt(top_title.title_right_colon, " ");}
 
    //os_printf("cld state:%d\n",pt_network_get());

    if(!pt_network_get())
    {
        midea_ui_safe_add_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
        midea_ui_safe_clear_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }
    else
    {
        // debug_d("\n pt_temperature_get = %d \n",pt_temperature_get());
        midea_ui_safe_add_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
        
        if(top_title.title_state == TITLE_SHOW_LEFT)
            return;
        // debug_d("\n pt_temperature_get = %d \n",pt_temperature_get());
        if(NOT_GET_VAL == pt_temperature_get()){
            lv_label_set_text_fmt(top_title.title_right_temp, "室外 --°C");
        }else{
            lv_label_set_text_fmt(top_title.title_right_temp, "室外%d°C", pt_temperature_get());
        }
        midea_ui_safe_clear_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
    }

    get_sys_time();

    if(min == pt_minute_get())
    {
        return ;
    }

    min = pt_minute_get();

    lv_label_set_text_fmt(top_title.title_hour, "| %02d", pt_hour_get());
    lv_label_set_text_fmt(top_title.title_min, "%02d", pt_minute_get());


}


int com_title_create(void)
{
    set_title_page_id(1);

    title_left();
    title_middle();
    title_right();

    #if 0
    if((pt_home_theme_get()==PT_THEME_DEVICE)&&(pt_dev_num_get()>=2)){
        com_title_show_all();
    }else{
        com_title_show_left();
    }
    #endif

    return 0;
}

int com_title_hide_all(void)
{
    top_title.title_state = TITLE_HIDE_ALL;
    if(!top_title.title_left_info)
        return -1;
    if(!top_title.top_icon)
        return -1;
    if(!top_title.title_right_temp)
        return -1;
    if(!top_title.wifi_icon)
        return -1;
    if(!top_title.title_hour)
        return -1;
    if(!top_title.title_min)
        return -1;
    if(!top_title.title_right_colon)
        return -1;

    if(!lv_obj_has_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN);
    }
    if(!lv_obj_has_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN);
    }
    if(!lv_obj_has_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
    }
    if(!lv_obj_has_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN);
    }   

    return 0;
}

int com_title_show_left(void)
{
    top_title.title_state = TITLE_SHOW_LEFT;
    if(!top_title.title_left_info)
        return -1;
    if(!top_title.top_icon)
        return -1;
    if(!top_title.title_right_temp)
        return -1;
    if(!top_title.wifi_icon)
        return -1;
    if(!top_title.title_hour)
        return -1;
    if(!top_title.title_min)
        return -1;
    if(!top_title.title_right_colon)
        return -1;

    if(lv_obj_has_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN);
    }
    if(lv_obj_has_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN);
    }

    if(!lv_obj_has_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
    }
    if(!lv_obj_has_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN);
    }  
    return 0;
}

int com_title_show_left_no_net(void)
{
    top_title.title_state = TITLE_SHOW_LEFT;
    if(!top_title.title_left_info)
        return -1;
    if(!top_title.top_icon)
        return -1;
    if(!top_title.title_right_temp)
        return -1;
    if(!top_title.wifi_icon)
        return -1;
    if(!top_title.title_hour)
        return -1;
    if(!top_title.title_min)
        return -1;
    if(!top_title.title_right_colon)
        return -1;

    if(lv_obj_has_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN);
    }
    if(lv_obj_has_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN);
    }

    if(!lv_obj_has_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
    }
    if(!lv_obj_has_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN);
    }   
    if(!lv_obj_has_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_add_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN);
    }  
    return 0;
}

int com_title_show_all(void)
{
    top_title.title_state = TITLE_SHOW_ALL;
    if(!top_title.title_left_info)
        return -1;
    if(!top_title.top_icon)
        return -1;
    if(!top_title.title_right_temp)
        return -1;
    if(!top_title.wifi_icon)
        return -1;
    if(!top_title.title_hour)
        return -1;
    if(!top_title.title_min)
        return -1;
    if(!top_title.title_right_colon)
        return -1;


    midea_ui_safe_clear_flag(top_title.title_left_info,LV_OBJ_FLAG_HIDDEN);
    
    midea_ui_safe_clear_flag(top_title.top_icon,LV_OBJ_FLAG_HIDDEN);
    

    debug_d("cld state3:%d\n",pt_network_get());

    if(!pt_network_get())
    {
        midea_ui_safe_add_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
        midea_ui_safe_clear_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }
    else
    {
        midea_ui_safe_clear_flag(top_title.title_right_temp,LV_OBJ_FLAG_HIDDEN);
        midea_ui_safe_add_flag(top_title.wifi_icon,LV_OBJ_FLAG_HIDDEN);
    }

    if(lv_obj_has_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.title_hour,LV_OBJ_FLAG_HIDDEN);
    } 
    if(lv_obj_has_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.title_min,LV_OBJ_FLAG_HIDDEN);
    } 
    if(lv_obj_has_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN))
    {
        midea_ui_safe_clear_flag(top_title.title_right_colon,LV_OBJ_FLAG_HIDDEN);
    }

    return 0;
}






